/*
 * DA7.c
 *
 * Created: 4/22/2014 6:22:48 PM
 *  Author: Omar Salazar
 */ 

#define F_CPU 8000000UL
#include <avr/pgmspace.h>
#include <avr/io.h> //standard AVR header
#include <util/delay.h> //delay header
#include "nokia5110.h"
//****************************************************************************
uint16_t ReadADC(uint8_t ADCchannel)
{
	ADMUX = (ADMUX & 0xF0) | (ADCchannel & 0x0F);//select ADC channel with safety mask
	ADCSRA |= (1<<ADSC);						//single conversion mode
	while( ADCSRA & (1<<ADSC) );				//wait until ADC conversion is complete
	return ADC;
}

int main(void)
{
	//Initialize ADC
	ADMUX |= (1<<REFS0);					  //Select Vref=AVcc
	ADCSRA |= (1<<ADPS1)|(1<<ADPS0)|(1<<ADEN);//set prescaller to 8 and enable ADC
	
	LcdInit();						//Initialize LCD
	LcdContrast(0x7F);				//Set the LCD to all black to ensure it is working
	_delay_ms(1000);				//leave it on for one second.
	LcdClear();						//then clear the LCD
	LcdContrast(0x3F);				//adjust the contrast 
	_delay_ms(1200);				
	LcdClear();
	LcdGotoXYFont(1,1);					//go to first line
	LcdFStr(FONT_1X,(unsigned char*)PSTR("The temperature"));	//print "The temperature"
	LcdUpdate();
	_delay_ms(1000);
	LcdGotoXYFont(2,1);					//go to the second line
	LcdFStr(FONT_1X,(unsigned char*)PSTR("is: "));	//print "is: "
	LcdUpdate();
	_delay_ms(1000);
	while(1)
	{	
		int temp = ReadADC(0)*5; //Read ADC value, LM34 conversion: 10mV/1 degree Fahrenheit.
		int d1 = temp%10;			//obtain the tens place
		int d2 = temp%100;			//obtain the ones place
		int d3 = temp%1000;			//obtain the tenths place
		int d4 = temp%10000;		//obtain the hundreths place
		char temperature[5];		//create an array of characters to print
		sprintf(temperature, "%d%d.%d%d", d1, d2, d3, d4);	//put the numbers in the correct format and store in the variable
		LcdGotoXYFont(2,2);					//go to second line, second column
		LcdFStr(FONT_1X, temperature);		//send the temperature to the LCD
		LcdUpdate();
		_delay_ms(1000);
		LcdGotoXYFont(2,3);					//go to the second line, third column
		LcdFStr(FONT_1X, (unsigned char*)PSTR("F"));	//print the F for fahrenheit
		LcdUpdate();
		_delay_ms(1000);
	}		

return 0;
}
